<script>
	var url;
	$(document).ready(function(){
	
		newData = function (){
			$('#dialog').dialog({
				title: 'Tambah Data Jenis Matakuliah',
				width: 475,
				height: 300,
				closed: true,
				cache: false,
				href: base_url+'jenis_mk/add',
				modal: true
			});
			 
			$('#dialog').dialog('open');
			url = base_url+'jenis_mk/save/add';
		}
		// end newData
		
		editData = function (val){
			$('#dialog').dialog({
				title: 'Edit Data Jenis Matakuliah',
				width: 475,
				height: 300,
				closed: true,
				cache: false,
				href: base_url+'jenis_mk/edit/'+val,
				modal: true
			});
			
			$('#dialog').dialog('open');  
			url = base_url+'jenis_mk/save/edit';
		}
		//end editData
		
		deleteData = function (val){
			if(confirm("Apakah yakin akan menghapus data '" + val + "'?")){
				var response = '';
				$.ajax({ type: "GET",
					 url: base_url+'jenis_mk/delete/' + val,
					 async: false,
					 success : function(response){
						var response = eval('('+response+')');
						if (response.success){
							$.messager.show({
								title: 'Success',
								msg: 'Data Berhasil Dihapus'
							});
							// reload and close tab
							$('#dg').datagrid('reload');
						} else {
							$.messager.show({
								title: 'Error',
								msg: response.msg
							});
						}
					 }
				});
			}
		}
		//end deleteData 
		
		saveData = function(){
			$('#form1').form('submit',{
				url: url,
				onSubmit: function(){
					return $(this).form('validate');
				},
				success: function(result){
					//alert(result);
					var result = eval('('+result+')');
					if (result.success){
						$('#dialog').dialog('close');		// close the dialog
						$('#dg').datagrid('reload');		// reload the user data
					} else {
						$.messager.show({
							title: 'Error',
							msg: result.msg
						});
					}
				}
			});
		}
		//end saveData
		
		actionbutton = function(value, row, index){
			var col;
			<?if($this->mdl_auth->CekAkses(array('menu_id'=>32 	, 'policy'=>'EDIT'))){?>
			col = '<a href="#" onclick="editData(\''+row.kd_jenismk+'\');" class="easyui-linkbutton" iconCls="icon-edit" plain="false">Edit</a>';
			<?}?>
			col += '&nbsp;&nbsp;|&nbsp;&nbsp;';
			<?if($this->mdl_auth->CekAkses(array('menu_id'=>32 	, 'policy'=>'DELETE'))){?>
			col += '&nbsp;&nbsp;|&nbsp;&nbsp;<a href="#" onclick="deleteData(\''+row.kd_jenismk+'\');" class="easyui-linkbutton" iconCls="icon-edit" plain="false">Delete</a>';
			<?}?>
			return col;
		}
		
		// load grid
		$(function(){
			$('#dg').datagrid({url:"<?=base_url()?>jenis_mk/grid"});	
			//$('#dg').datagrid('enableFilter'); 			
		});	
		
		// onchange
		$('#s_kd_pt').change(function(){
			$('#s_kd_fakultas').load(base_url+'prosedur/getFakultasbyUniversitas/'+$('#s_kd_pt').val());
		});
		
		$('#s_kd_fakultas').change(function(){
			$('#s_kd_prodi').load(base_url+'prosedur/getProdibyFakultas/'+$('#s_kd_fakultas').val());
		});
		
		// filter
		filter = function(){
			$('#dg').datagrid('load',{
				kd_pt : $('#s_kd_pt').val(),
				kd_fakultas : $('#s_kd_fakultas').val(),
				kd_prodi : $('#s_kd_prodi').val(),
			});
			//$('#dg').datagrid('enableFilter');
		}
		
		//# Tombol Bawah
		$(function(){
			var pager = $('#dg').datagrid().datagrid('getPager');	// get the pager of datagrid
			pager.pagination({
				buttons:[
					{
						iconCls:'icon-add',
						text:'Tambah Data',
						handler:function(){
							newData();
						}
					},
					{
						iconCls:'icon-print',
						text:'Export PDF',
						handler:function(){
							window.open('<?=base_url().'jenis_mk/pdf'?>');
						}
					}
				]
			});			
		});
		
	});
</script>
<table id="dg" title="Kelola Data Jenis Matakuliah" data-options="
			rownumbers:true,
			singleSelect:true,
			autoRowHeight:false,
			pagination:true,
			pageSize:30,
			fit:true,
			toolbar:'#toolbar',
			">
	<thead>
		<tr>
			<th field="kd_jenismk" sortable="true" width="100">Kode</th>
			<th field="Urutan" sortable="true" width="100">Urutan</th>
			<th field="Singkatan" sortable="true" width="100">Singkatan</th>
			<th field="Nama" sortable="true" width="200">Nama</th>
			<th field="action" align="center" formatter="actionbutton" width="100">Aksi</th>
		</tr>
	</thead>
</table>
<div id="toolbar" style="padding:5px;height:auto">
	<div style="margin-bottom:5px">
		
	</div>
	<div>
		<table width="700" border="0">
		   <!--<tr>
			<td>Perguruan Tinggi</td>
			<td>: 
				<select id="s_kd_pt" name="s_kd_pt" style="width:290px;">
					<?=$this->mdl_prosedur->OptionUniversitas();?>
				</select>
			</td>
		  </tr>-->
		  <tr>
			<td>Fakultas</td>
			<td>: 
				<select id="s_kd_fakultas" name="s_kd_fakultas" style="width:290px;">
					<?=$this->mdl_prosedur->OptionFakultasByUniv();?>
				</select>
			</td>
		  </tr>
		  <tr>
			<td>Program Studi</td>
			<td>: 
				<select id="s_kd_prodi" name="s_kd_prodi" style="width:290px;">
					<?=$this->mdl_prosedur->OptionProdi();?>
				</select>
			</td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td>&nbsp;&nbsp;<a href="#" onclick="filter()" class="easyui-linkbutton" iconCls="icon-search">Filter</a></td>
		  </tr>
		</table>
	</div>
</div>
